package com.etogc.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;

import com.etogc.groupchat.R;
import com.etogc.groupchat.bean.AllGroupBean;
import com.etogc.groupchat.bean.MessageEvent;
import com.etogc.http.ReqErrorHandler;
import com.etogc.http.VolleyHttpUtil;
import com.etogc.groupchat.common.CommonSp;
import com.etogc.util.GcUrl;
import com.etogc.util.YcToastHelper;
import com.etogc.view.LoadDialog;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;

public class InputCodeDialog extends Dialog {

    private Context mContext;

    private AllGroupBean.AllGroup allGroupItem;

    private InputCodeDialog mInputCodeDialog;

    public InputCodeDialog(Context context, int theme) {
        super(context, theme);
    }

    public InputCodeDialog(Context context, AllGroupBean.AllGroup allGroup) {
        super(context);
        this.mContext = context;
        this.allGroupItem = allGroup;
    }

    public InputCodeDialog initDialog() {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInputCodeDialog = new InputCodeDialog(mContext, R.style.MyDialog);
        View layout = inflater.inflate(R.layout.dialog_input_code, null);
        mInputCodeDialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        final EditText etCode = layout.findViewById(R.id.et_invite_code);

        layout.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mInputCodeDialog.dismiss();
            }
        });

        layout.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    YcToastHelper.getInstance()._toast("请填写邀请码");
                } else {
                    joinGroup(code);
                    mInputCodeDialog.dismiss();
                }

            }
        });
        mInputCodeDialog.setContentView(layout);
        return mInputCodeDialog;
    }

    /**
     * 加入群聊
     *
     * @param inviteCode
     */
    public void joinGroup(String inviteCode) {
        LoadDialog.show(mContext);
        HashMap<String, Object> map = new HashMap<>();
        map.put("group_id", allGroupItem.getId());
        map.put("invite_code", inviteCode);
        map.put("user_id", CommonSp.getUserID());
        map.put("type", "0");//0=app,1=pc
        VolleyHttpUtil.getInstance().postWithCallback(GcUrl.JOIN_GROUP, map, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                LoadDialog.dismiss(mContext);
                boolean isOk = ReqErrorHandler.getInstance().isSuccess(response, true);
                Log.e("Biz", response.toString());
                if (isOk) {
                    YcToastHelper.getInstance()._toast("加入群聊成功");
                    EventBus.getDefault().post(new MessageEvent("refresh"));
                }
            }
        });
    }
}
